package org.gdstash.ui;

import org.gdstash.db.DBEngineLevel;
import org.gdstash.db.DBEnginePlayer;
import org.gdstash.ui.character.GDCharMasteryPane;
import org.gdstash.ui.character.GDUIMasterySupport;
import org.gdstash.ui.util.AdjustableTabbedPane;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import java.awt.*;

public class GDMasteryInfoPane extends AdjustableTabbedPane implements GDUIMasterySupport {
    private static int MAX_POINTS;
    private static int MAX_LEVEL;
    private static final int QUEST_POINTS = 11;
    private GDCharMasteryPane pnlMastery1;
    private GDCharMasteryPane pnlMastery2;
    private int points;

    public static void initStats() {
        GDMasteryInfoPane.MAX_LEVEL = 100;
        GDMasteryInfoPane.MAX_POINTS = 248;
        final DBEnginePlayer ep = DBEnginePlayer.get();
        if (ep != null) {
            GDMasteryInfoPane.MAX_LEVEL = ep.getMaxLevel();
            final DBEngineLevel el = DBEngineLevel.get();
            if (el != null) {
                GDMasteryInfoPane.MAX_POINTS = el.getSkillPointsForRange(1, GDMasteryInfoPane.MAX_LEVEL) + 11;
            }
        }
    }

    public GDMasteryInfoPane(final GDStashFrame frame) {
        this.points = GDMasteryInfoPane.MAX_POINTS;
        this.adjustUI();
    }

    @Override
    public void adjustUI() {
        final Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntTabbed = UIManager.getDefaults().getFont("TabbedPane.font");
        fntTabbed = fntTabbed.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        this.setFont(fntTabbed);
        if (this.pnlMastery1 == null) {
            (this.pnlMastery1 = new GDCharMasteryPane(this, true)).setSkillPoints(this.points);
            this.add(this.pnlMastery1);
        } else {
            this.pnlMastery1.adjustUI();
        }
        if (this.pnlMastery2 == null) {
            (this.pnlMastery2 = new GDCharMasteryPane(this, true)).setSkillPoints(this.points);
            this.add(this.pnlMastery2);
        } else {
            this.pnlMastery2.adjustUI();
        }
        int index = 0;
        this.setTitleAt(index, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "HEADER_MASTERY_1"));
        ++index;
        this.setTitleAt(index, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "HEADER_MASTERY_2"));
        ++index;
    }

    public void updateConfig() {
        this.setSkillPoints(GDMasteryInfoPane.MAX_POINTS);
        if (this.pnlMastery1 != null) {
            this.pnlMastery1.updateConfig();
        }
        if (this.pnlMastery2 != null) {
            this.pnlMastery2.updateConfig();
        }
    }

    @Override
    public int getSkillPoints() {
        return this.points;
    }

    @Override
    public void setSkillPoints(final int points) {
        this.points = points;
        this.pnlMastery1.setSkillPoints(points);
        this.pnlMastery2.setSkillPoints(points);
    }

    @Override
    public int getPointsForSkill(final String skillID) {
        int points = this.pnlMastery1.getPointsForSkill(skillID);
        if (points > 0) {
            return points;
        }
        points = this.pnlMastery2.getPointsForSkill(skillID);
        return points;
    }

    @Override
    public int getCharLevel() {
        return GDMasteryInfoPane.MAX_LEVEL;
    }

    @Override
    public void resetMastery(final String masteryID) {
    }
}
